package cn.tedu._05mvcboot02.controller;


import cn.tedu._05mvcboot02.pojo.dto.UserUpdataDTO;
import cn.tedu._05mvcboot02.pojo.entity.User;
import cn.tedu._05mvcboot02.mapper.UserMapper;
import cn.tedu._05mvcboot02.pojo.dto.UserInsertDTO;
import cn.tedu._05mvcboot02.pojo.vo.UserinfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

//@Controller
@RestController
@RequestMapping("/v1/users/")
public class UserController {

    @Autowired
    private UserMapper userMapper;


    @PostMapping("insert")
//    @ResponseBody
    public String insert(UserInsertDTO userInsertDTO){

        /*
        *   梳理逻辑，确定接口
        *   1.接收客户端传递的数据
        *   2.数据合法化校验
        *   3.确定用户名是否被占用【select】
        *       3.1被占用，返回信息
        *       3.2没被占用，插入数据【INSERT】，返回信息
         */

        User user = new User();
        BeanUtils.copyProperties(userInsertDTO,user);
        user.setCreated(new Date());

        userMapper.insertUser(user);


        return "添加成功";
    }


    @GetMapping("delete")
//    @ResponseBody
    public String Delete(int id){

        userMapper.deleteUser(id);
        return "删除成功";
    }


    @PostMapping("update")
//    @ResponseBody
    public String update(User user){
        userMapper.updateUser(user);
        return "修改成功";
    }

//    @GetMapping("{id}")
//    public UserinfoVO getUserinfo(@PathVariable int id){
//        return userMapper.selectUserinfoById(id);











}
